package com.caption.jdk.stream;

import com.caption.jdk.lambda.Person;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 *  collection 数据收集
 */
public class TestStream04 {

    public static void main(String[] args) {
        /**
         * 收集成list
         */
        List<Person> list = Stream.of("张三", "李四", "王五").map(st -> new Person(st)).collect(Collectors.toList());
        System.out.println(list);
        /**
         * 收集成set
         */
        Set<String> set = Stream.of("a", "b", "c", "a").collect(Collectors.toSet());
        System.out.println(set);
        /**
         * 收集成ArrayList
         */
        ArrayList<String> arrayList = Stream.of("a", "b", "c", "a")
                //.collect(Collectors.toCollection(() -> new ArrayList<>()));
                .collect(Collectors.toCollection(ArrayList::new));  //改成方法引用
        System.out.println(arrayList);
        /**
         * 收集成hashSet
         */
        HashSet<String> hashset = Stream.of("a", "b", "c", "a").collect(Collectors.toCollection(HashSet::new));
        System.out.println(hashset);

        /**
         * 转换成数组
         */
        String[] strings = Stream.of("a", "b", "c", "a").toArray(String[]::new);
        System.out.println(Arrays.toString(strings));

    }
}
